package com.biboheart.adapter.support.utils;

import org.springframework.core.convert.support.DefaultConversionService;

import java.util.regex.Pattern;

public class ObjectTypeUtils {
    public static <T> T convertType(Object source, Class<T> valueType) {
        if (null == source) {
            return null;
        }
        return DefaultConversionService.getSharedInstance().convert(source, valueType);
    }

    public static boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("^[\\d]*$");
        return pattern.matcher(str).matches();
    }

    public static boolean isPrimitive(Object source) {
        if (null == source) return true;
        if (source.getClass().isPrimitive()) return true;
        return source instanceof Integer
                || source instanceof Byte
                || source instanceof Long
                || source instanceof Double
                || source instanceof Float
                || source instanceof Character
                || source instanceof Short
                || source instanceof Boolean
                || source instanceof String;
    }

    public static boolean isPrimitive(Class<?> clazz) {
        if (null == clazz) return true;
        if (clazz.isPrimitive()) return true;
        return Integer.class.equals(clazz)
                || Byte.class.equals(clazz)
                || Long.class.equals(clazz)
                || Double.class.equals(clazz)
                || Float.class.equals(clazz)
                || Character.class.equals(clazz)
                || Short.class.equals(clazz)
                || Boolean.class.equals(clazz)
                || String.class.equals(clazz);
    }
}
